package main

import (
	"encoding/json"
	"fmt"
	"strconv"
	"testing"
	"time"
)

//数组
func String(b *testing.B) {
	b.ReportAllocs()
	b.ResetTimer()
	string :=[3]string{"name","sex","test"}
	for _,v :=range string{
		fmt.Sprintf("%v", v)
	}
}


/**
工厂模式
*/
type Shape interface {
	Draw()
}

type Rectangle struct {
}

func (this Rectangle) Draw() {
	fmt.Println("Inside Rectangle::draw() method.")
}



type Square struct {
}

func (this Square) Draw() {
	fmt.Println("Inside Square ::draw() method.")
}



type Circle struct {
}

func (this Circle) Draw() {
	fmt.Println("Inside Circle  ::draw() method.")
}

type ShapeFactory struct {
}

//获取形状类型的对象
func (this ShapeFactory) getShape(shapeType string) Shape {

	if shapeType == "" {
		return nil
	}
	if shapeType == "CIRCLE" {
		return Circle{}
	} else if shapeType == "RECTANGLE" {
		return Rectangle{}
	} else if shapeType == "SQUARE" {
		return Square{}
	}
	return nil
}

//并集
func Union(slice1, slice2 []string) []string {
	m := make(map[string]int)
	for _, v := range slice1 {
		m[v]++
	}

	for _, v := range slice2 {
		times, _ := m[v]
		if times == 0 {
			slice1 = append(slice1, v)
		}
	}
	return slice1
}

//求交集
func Intersect(slice1, slice2 []string) []string {
	m := make(map[string]int)
	nn := make([]string, 0)
	for _, v := range slice1 {
		m[v]++
	}

	for _, v := range slice2 {
		times, _ := m[v]
		if times == 1 {
			nn = append(nn, v)
		}
	}
	return nn
}

//差集
func Difference(slice1, slice2 []string) []string {
	m := make(map[string]int)
	nn := make([]string, 0)
	inter := Intersect(slice1, slice2)
	for _, v := range inter {
		m[v]++
	}

	for _, value := range slice1 {
		times, _ := m[value]
		if times == 0 {
			nn = append(nn, value)
		}
	}
	return nn
}

/**
深拷贝
实现方式：递归拷贝
*/
func Copy(value interface{}) interface{} {
	if valueMap, ok := value.(map[string]interface{}); ok {
		newMap := make(map[string]interface{})
		for k, v := range valueMap {
			newMap[k] = Copy(v)
		}

		return newMap
	} else if valueSlice, ok := value.([]interface{}); ok {
		newSlice := make([]interface{}, len(valueSlice))
		for k, v := range valueSlice {
			newSlice[k] = Copy(v)
		}

		return newSlice
	}

	return value
}

/**
去重
*/
func Remove(addrs []string) []string {
	result := make([]string, 0, len(addrs))
	temp := map[string]struct{}{}
	for _, item := range addrs {
		if _, ok := temp[item]; !ok {
			temp[item] = struct{}{}
			result = append(result, item)
		}
	}
	return result
}

/**
可变参数
*/
func Sum(nums ...int) {
	fmt.Print(nums, " ")
	total := 0
	for _, num := range nums {
		total += num
	}
	fmt.Println(total)
}

//多态
type baseRoom struct {
	RoomName string
}

//这里继承房间类
type myRoom struct {
	myRoomName string
	baseRoom
}
type iroom interface {
	getName()
	setName(string)
}

//给room实现方法
func (p* baseRoom) getName() {
	fmt.Printf("my room name is %s\r\n",p.RoomName)
}

func (p* baseRoom) setName(a string) {
	p.RoomName = a
}

//json转map
func JsonToMap() {
	jsonStr := `
        {
                "name": "hj",
                "sex": 1
        }
        `
	var mapResult map[string]interface{}
	err := json.Unmarshal([]byte(jsonStr), &mapResult)
	if err != nil {
		fmt.Println("JsonToMap err: ", err)
	}
	fmt.Println(mapResult)
}

//map转json
func MapToJson() {
	str, _ := json.Marshal(map[string]int{"age":1, "sex":12})
	fmt.Println(string(str))

}

//jsonToStruct
type People struct {
	Name string `json:"name"`
	Sex int `json:"sex"`
}

func JsonToStruct(){
	var people People
	jsonStr := `
        {
                "name": "hj"
                "sex":1
        }
        `
	json.Unmarshal([]byte(jsonStr), &people)
	fmt.Println(people)
}

//struct to json
func StructToJson(){
	p := People{
		Name: "hj",
		Sex: 1,
	}

	jsonBytes, err := json.Marshal(p)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(string(jsonBytes))
}


func MapToJson2() {
	mapInstances := []map[string]interface{}{}
	instance_1 := map[string]interface{}{"name": "hj", "sex": 1}
	instance_2 := map[string]interface{}{"name": "test", "sex": 2}
	mapInstances = append(mapInstances, instance_1, instance_2)

	jsonStr, err := json.Marshal(mapInstances)

	if err != nil {
		fmt.Println("MapToJson err: ", err)
	}
	fmt.Println(string(jsonStr))
}

//日期转化
func DataChange() {
	date := "2021-05-01 00:00:00"

	//日期转化为时间戳
	timeLayout := "2021-08-12 12:00:00"
	loc, _ := time.LoadLocation("Local")
	tmp, _ := time.ParseInLocation(timeLayout, date, loc)
	timestamp := tmp.Unix()
	fmt.Println(timestamp)

	//时间戳转化为日期
	date = time.Unix(timestamp, 0).Format(timeLayout)
	fmt.Println(date)
}

//StringToByte
func StringToByte() {
	var str  = "test"
	var res  = []byte(str)
	fmt.Println(res)
}
//ByteToString
func ByteToString() {
	var res = []byte("test")
	var str = string(res[:])
	fmt.Println(str)
}

//stringToint
func StringToInt() {
	str := "1"
	num, _ := strconv.Atoi(str)
	fmt.Println(num)
	num64,_ := strconv.ParseInt(str, 10, 64)
	fmt.Println(num64)
}

func IntToString() {
	num := 11
	var num64 int64
	num64 =  11111
	str := strconv.Itoa(num)
	fmt.Println(str)
	str64 :=strconv.FormatInt(num64,10)
	fmt.Sprintf("%v",str64)
}


func main() {
	//工厂模式输出
	factory := ShapeFactory{}
	factory.getShape("CIRCLE").Draw()
	factory.getShape("RECTANGLE").Draw()
	factory.getShape("SQUARE").Draw()
	//并集，交集，差集
	slice1 := []string{"1", "2", "3", "6", "8"}
	slice2 := []string{"2", "3", "5", "0"}
	un := Union(slice1, slice2)
	fmt.Println("slice1与slice2的并集为：", un)
	in := Intersect(slice1, slice2)
	fmt.Println("slice1与slice2的交集为：", in)
	di := Difference(slice1, slice2)
	fmt.Println("slice1与slice2的差集为：", di)
	//去重
	s := []string{"hello", "hello", "hello","name","name","sex","age","sex"}

	fmt.Println(Remove(s))

	//多态
	mymap := make(map[string]iroom)
	mymap["test"] = &myRoom{}
	mymap["test"].setName("test的房间")
	mymap["test"].getName()
	mymap["test"].(*myRoom).RoomName = "hj的房间"

	//转化
	JsonToMap()
	MapToJson()
	MapToJson2()
	JsonToStruct()
	StructToJson()
	DataChange()
	StringToByte()
	ByteToString()
	StringToInt()
	IntToString()
}